package com.yxx.service.LogService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxx.entity.ExceptionLog;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.query.LogQuery;
import com.yxx.mapper.ExceptionLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 异常日志服务
 * @author yuxiangxun
 * @date 2025/2/15
 * @apiNote
 */
@Slf4j
@Service("exceptionLogService")
public class ExceptionLogService extends ServiceImpl<ExceptionLogMapper,ExceptionLog> {

    @Resource
    private ExceptionLogMapper exceptionLogMapper;

    public PageResult<ExceptionLog> listExceptionLog(LogQuery logQuery){
        // 查询异常日志数量
        Long count = exceptionLogMapper.selectCount(new LambdaQueryWrapper<ExceptionLog>()
                .like(StringUtils.hasText(logQuery.getKeyword()), ExceptionLog::getDescription, logQuery.getKeyword())
                .or()
                .like(StringUtils.hasText(logQuery.getOptModule()), ExceptionLog::getModule, logQuery.getOptModule())

        );
        // 如果查询的数量为0，返回一个空的分页结果对象
        if(count == 0){
            return new PageResult<>();
        }
        // 如果查询的数量不为0，分页查询异常日志列表并返回
        List<ExceptionLog> exceptionLogList = exceptionLogMapper.selectExceptionLogList(logQuery);
        return new PageResult<>(exceptionLogList, count);
    }


    public void saveExceptionLog(ExceptionLog exceptionLog) {
        // 保存异常日志
        exceptionLogMapper.insert(exceptionLog);
    }
}
